<?php
// +----------------------------------------------------------------------
// | HkCms 插件管理
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\addons\util;

use common\controller\AdminBase;

class Adminaddonbase extends AdminBase {

    //插件标识
    public $addonName = NULL;
    //插件基本信息
    protected $addonInfo = NULL;
    //插件路径
    protected $addonPath = NULL;

    public function _initialize() {
        parent::_initialize();
        $this->addonName = request()->controller();
        $this->addonInfo = model('addons/Addons')->where(array('name' => $this->addonName))->find();
        if (empty($this->addonInfo)) {
            $this->error('该插件没有安装！');
        }
        if (!$this->addonInfo['status']) {
            $this->error('该插件已被禁用！');
        }
        $this->addonPath = model('addons/Addons')->getAddonsPath() . strtolower($this->addonName) . '/';
    }

    /**
     * 获取插件名称
     * @return type
     */
    final public function getAddonName() {
        return $this->addonName;
    }

    /**
     * 获取插件配置
     * @staticvar array $_config
     * @return type
     */
    final public function getAddonConfig() {
        $config = $this->addonInfo['config'];
        if ($config) {
            //反序列化
            $config = unserialize($config);
        }
        return $config;
    }

    /**
     * 模板显示
     * 此方法作用在于实现后台模板直接存放在各自项目目录下。例如Admin项目的后台模板，直接存放在Admin/Tpl/目录下
     * @param string $templateFile
     * @param array $vars
     * @param array $replace
     * @param array $config
     * @return mixed|void
     */
    public function display($templateFile = '', $vars = [], $replace = [], $config = []) {
        return parent::fetch(parseAddonTemplateFile($templateFile, $this->addonPath), $replace, $config);
    }

    public function fetch($templateFile = '', $vars = [], $replace = [], $config = []) {
        return parent::fetch(parseAddonTemplateFile($templateFile, $this->addonPath), $replace, $config);
    }
}
